package com.tqjc.system.core.pipeline.thread;

import com.tqjc.system.core.threadpool.ThreadPoolManager;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @description: 特别的线程唤醒线程
 * @Author: 86195
 * @Date: 2024/1/3 20:35
 * @param:
 * @return:
 **/
@Slf4j
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class ExtraTaskWakeThread extends Thread{

    private CarDoingAddTaskThread thread;

    private String carInfo;
    @Override
    public void run() {
        try {
            while (thread.isSecAvoidFlag()){
                log.info("天车需要执行二次避让，循环判断线程是否等待中。。。。");
                //处理二次避让中
                if(thread.getState() == State.WAITING){
                    CarProgressLogThread msgThread = CarProgressLogThread.builder().carInfo(carInfo)
                            .msg("该天车睡眠动作被特殊唤醒线程唤醒").build();
                    ThreadPoolManager.getInstance().execute(msgThread);
                    synchronized (thread){
                        log.info("天车需要执行二次避让，第一次避让已完成，唤醒其线程");
                        thread.notify();
                        return;
                    }
                }

                if(thread.isTaskFinishFlag() || thread.isTaskFlag()){
                    synchronized (thread){
                        log.warn("天车需要执行二次避让循环中，执行业务线程的出现异常，线程结束。任务结束标识为：{}。点位监控异常标识为：{}",thread.isTaskFinishFlag(),thread.isTaskFlag());
                        thread.notify();
                        return;
                    }

                }
                Thread.sleep(1*1000);
            }

        } catch (Exception e){
            log.error("二次额外唤醒线程运行异常",e);
        }
    }
}
